import { defineStore } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import type {
  aqiFeedbackType,
  aqiType,
  monitorType,
  inspectorType,
  aqiMeasuredResultType
} from "../types";
import { store } from "../utils";

export const useFeedbackInfoStore = defineStore({
  id: "feedbackInfo",
  state: (): {
    aqiFeedback: aqiFeedbackType;
    monitor: monitorType;
    imageUrl: string[];
    aqi: aqiType;
    inspector: inspectorType;
    aqiValue: number;
    result: aqiMeasuredResultType;
  } => ({
    aqiFeedback: {
      fbId: 0,
      monId: 0,
      fbInfo: "",
      fbAqiLevel: 0,
      fbTime: "",
      fbImg: "",
      fbAssignState: 0,
      fbAssignTime: "",
      province: "",
      city: "",
      fbAddress: "",
      remarks: ""
    },
    monitor: {
      monId: 0,
      monTel: 0,
      monPassword: "",
      monName: "",
      monSex: 0,
      monBirthday: "",
      province: "",
      city: "",
      monAddress: "",
      remarks: ""
    },
    imageUrl: [],
    aqi: {
      aqiLevel: 0,
      aqiColor: "",
      aqiLevelDes: "",
      aqiHealthImpact: "",
      aqiTakeMeasure: ""
    },
    inspector: {
      insId: 0,
      insName: "",
      insPassword: "",
      insAddress: "",
      province: "",
      city: "",
      insState: 1,
      insTel: 0,
      remarks: ""
    },
    aqiValue: 0,
    result: {
      aqimrId: 0,
      monId: 0,
      fbId: 0,
      insId: 0,
      aqimrState: 1,
      aqimrAddress: "",
      aqimrCoValue: 0,
      aqimrCoLevel: 0,
      aqimrSo2Value: 0,
      aqimrSo2Level: 0,
      aqimrPmValue: 0,
      aqimrPmLevel: 0,
      aqimrMeasuredTime: "",
      aqimrInfo: "",
      province: "",
      city: "",
      remarks: ""
    }
  }),
  actions: {
    SET_AQIFEEDBACK(aqiFeedback: aqiFeedbackType) {
      this.aqiFeedback = aqiFeedback;
    },
    SET_MONITOR(monitor: monitorType) {
      this.monitor = monitor;
    },
    SET_IMAGEURL(imageUrl: string[]) {
      this.imageUrl = imageUrl;
    },
    SET_AQI(aqi: { aqi: AqiGradeType }) {
      this.aqi = aqi;
    },
    SET_INSPECTOR(inspector: inspectorType) {
      this.inspector = inspector;
    },
    SET_AQIVALUE(aqiValue: number) {
      this.aqiValue = aqiValue;
    },
    SET_RESULT(result: aqiMeasuredResultType) {
      this.result = result;
    }
  },
  persist: true
});

store.use(piniaPluginPersistedstate);

export function useFeedbackInfoStoreHook() {
  return useFeedbackInfoStore(store);
}
